<?php
/*
 * 用户管理
 */

namespace Dms\Controller;
use Model\UserAuth;

use Home\Controller\CommonController;

class UserController extends CommonController
{
    public function lists()
    {   
        $id = I("get.id",0);
        $group = M('auth_group')->find($id);
        
        if(empty($group))
        {
        	$this->error('错误操作');
        }

		$ids = M('auth_group_access')->where(array('group_id' => $id))->getField('uid',true);
        $where = array();
        $where['id'] = array('in',implode(',',$ids));
            
        $count = M('user')->where($where)->count();
        $Page = new \Think\Page($count,10);
        $user = M('user')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
 
        $this->user = $user;
        $this->page = $Page->show();
        $this->menuID = 7;
        $this->menuName = '用户列表';
    	$this->display();
    }
    
    public function addUser()
    {
    	if(IS_POST)
    	{
    		$username = I("post.username");
    		$password = I("post.password");
    		$group      = I("post.group");
    		$companyName = I("post.companyName");
    		$name                 = I("post.name");
    		$tel                      = I("post.tel");
    		$email                 = I("post.email");
    		$qq                     = I("post.qq");
    		$remark              = I("post.remark");
    		$pioneer               = I("post.pioneer");
    
    		$where = array();
    		$where['userName'] = $username;
    		
    		$user = M('user')->where($where)->find();
    		if(empty($user) == true)
    		{
    		    $Crypt = new \Model\Crypt();
    		    $md5password = $Crypt->singlEncrypt($password);
    		    $data = array();
    		    $data['userName'] = $username;
    		    $data['companyName'] = $companyName;
    		    $data['name']        = $name;
    		    $data['tel']             = $tel;
    		    $data['email']       = $email;
    		    $data['qq']           = $qq;
    		    $data['remark']    = $remark;
    		    $data['pioneer']       = $pioneer;
    		    $data['passWord'] = $md5password['encrypt'];
    		    $data['publicKey'] = $md5password['key'];
    		    $data['registerTime'] = time();
    		    $data['registerIp'] = ip2long(getIP());
    		    $data['registerBy'] = $this->getIdenty();
    		    	
    		    $insertId = M('user')->add($data);
    		    	
    		    M('auth_group_access')->add(array('uid'=>$insertId,'group_id'=>$group));
    		   $this->success('添加用户成功！');
    		   
    		   
    		}
    		else
    		{
    		    
    		    $this->error('账号已经存在');
    		}

    		exit;
    	}
    	
    	$this->group = M('auth_group')->select();
    	$this->menuID = 7;
    	$this->menuName = '添加用户';
    	$this->display();
    }
    
    public function editUser()
    {
    	$id = I("get.id",0);
    	$user = M('user')->find($id);
    	
    	if(empty($user) == true)
    	{
    		$this->error('错误操作');
    	}
    	
    	$groupBy = M('auth_group_access')->where(array('uid'=>$id))->getField('group_id');
    	
    	if(IS_POST)
    	{
    		$password = I("post.password");
    		$companyName = I("post.companyName");
    		$name                 = I("post.name");
    		$tel                      = I("post.tel");
    		$email                 = I("post.email");
    		$qq                     = I("post.qq");
    		$remark              = I("post.remark");
    		$pioneer               = I("post.pioneer");
    		$group = I("post.group");
    		$islock = I("post.islock");
    		//$orderDestination = I("post.orderDestination");
    		//$groupFlag = I("post.groupFlag");
    		
    		
    		if(empty($password) == false)
    		{
    			$UserAuth = new UserAuth();
    			$UserAuth->changePassword($id, $password);
    		}
    		
    		if($group != $groupBy)
    		{
    			M('auth_group_access')->where(array('uid'=>$id))->save(array('group_id'=>$group));
    		}

    		if($islock != $user['isLock'])
    		{
    			M('user')->where(array('id'=>$id))->save(array('isLock'=>$islock));
    		}
    		
    		M('user')->where(array('id'=>$id))->save(array('companyName'=>$companyName,'name'=>$name,'tel'=>$tel,'email'=>$email,'qq'=>$qq,'remark'=>$remark,'pioneer'=>$pioneer,'isLock'=>$islock));

    		$this->success('修改成功！');
    		
    		exit;
    	}
    	
    	$groups = M('auth_group')->select();

    	foreach ($groups as $key => $value)
    	{
    		if($value['id'] == $groupBy)
    		{
    			$groups[$key]['selected'] = ' selected="selected" ';
    		}
    	}
    	
    	$area = M('country')->Field('name,id')->where(array('pid'=>0))->select();
    	$orderDestination = explode(",", $user['orderDestination']);
    	foreach ($area as $key => $value) 
    	{
    		$country = M('country')->Field('name,id')->where(array('pid'=>$value['id']))->select();
    		foreach ($country as $key1 => $value1)
    		{
    			if(in_array($value1['id'], $orderDestination))
    			{
    				$country[$key1]['checked'] = 'checked="checked"';
    			}
    		}
    		$area[$key]['country'] = $country;
    	}
    	
//     	foreach ($area as $key => $value)
//     	{
//     		foreach($value['country'] as $key1 => $value1)
//     		{
//     			$area[$key]['country'][$key1]['city'] = M('country')->Field('name,id')->where(array('pid'=>$value1['id']))->select();
//     		}
//     	}

    	$this->group = $groups;
    	$this->area = $area;
    	$this->user = $user;
    	$this->menuID = 7;
    	$this->menuName = '编辑用户';
    	$this->display();
    }
    
    public function changePassword()
    {
    	$id = getIdenty();
    	
    	if(IS_POST)
    	{
    		$oldpassword = I("post.oldpassword");
    		$password = I("post.password");
    		$repassword = I("post.repassword");
    		
    		if(empty($oldpassword) == true || empty($password) == true || empty($repassword) == true)
    		{
    			$this->error('请确认必填项');
    		}
    		
    		if($password != $repassword)
    		{
    			$this->error('两次密码不一致');
    		}
    		
    		$user = M('user')->find($id);
    		
    		if($user['passWord'] != $oldpassword)
    		{
    			$this->error('旧密码错误');
    		}
    		
    		$UserAuth = new UserAuth();
    		$UserAuth->changePassword($id, $password);
    		
    		$this->success('修改密码成功！');
    		exit;
    	}
    	
    	$this->display();
    }
 
    public function edit()
    {
    	$id = getIdenty();
    	$user = M('user')->find($id);
    	 
    	if(empty($user) == true)
    	{
    		$this->error('错误操作');
    	}
    	 
    	$groupBy = M('auth_group_access')->where(array('uid'=>$id))->getField('group_id');
    	$user['group'] = M('auth_group')->where(array('id'=>$groupBy))->getField('title');

    	if(IS_POST)
    	{
    		$password = I("post.password");
    	
    		if(empty($password) == false)
    		{
    			$UserAuth = new UserAuth();
    			$UserAuth->changePassword($id, $password);
    		}

    		$this->success('修改成功！');
    	
    		exit;
    	}

    	$this->group = $groups;
    	$this->user = $user;
		$this->menuID = 7;
    	$this->menuName = '编辑个人资料';
    	$this->display();
    }
    public function getIdenty()
    {
        $uid = session('uid');
        if($uid == null) $uid = false;
        return $uid;
    }
}